<?php

require('configs/include.php');
require('configs/gvar.php');

require(C_FULL_PATH . 'modules/m_phpass/PasswordHash.php');

class c_index extends super_controller {

    //save in session user data
    public function set_user_session($empleado) {
        $_SESSION['empleado']['nombre'] = $empleado->get('nombre');
        $_SESSION['empleado']['cedula'] = $empleado->get('cedula');
        $_SESSION['empleado']['cargo'] = $empleado->get('cargo');
        $_SESSION['empleado']['login']='Si';
        $this->session = $_SESSION;
        
        
    }

    //destroy session
    public function destroy_session() {
        session_destroy();
        unset($this->session);
        $_SESSION['empleado']['login']='No';
    }

    public function login() {
        if (is_empty($this->post->cedula)) {
            throw_exception("The cedula can't be empty");
        } else if (is_empty($this->post->contrasena)) {
            throw_exception("The password can't be empty");
        }
        $cod['empleado']['cedula'] = $this->post->cedula;
        $cod['empleado']['contrasena'] = $this->post->contrasena;
        $options['empleado']['lvl2'] = "one_login";
               
        $this->orm->connect();
        $this->orm->read_data(array("empleado"), $options, $cod);
        $empleado = $this->orm->get_objects("empleado");
        $this->orm->close();
         $this->session['empleado']['login']='Si';
       
 
        
        if (empty($empleado[0])) {
            throw_exception($this->gvar['m_incorrect_login']);
            $this->session['empleado']['login']='No';
        } else {
            $this->set_user_session($empleado[0]);
            $this->msg_warning = "Login Successful";
            $this->type_warning = "success";
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
           
            
        }
            }

    public function logout() {
        $this->session['empleado']['login']='No';
        $this->destroy_session();
        $this->type_warning = "success";
        $this->msg_warning = "Logout Complete";
        $this->engine->assign('type_warning', $this->type_warning);
        $this->engine->assign('msg_warning', $this->msg_warning);
        $this->temp_aux = 'message.tpl';        
     
    }
    public function display() {
         $this->engine->assign('title', $this->gvar['n_global']);
        $this->engine->assign('active', $this->gvar['n_index']);
        
        $this->engine->display('header.tpl');
        
       

        if ($this->error == 1) {
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
        }

        $this->engine->display($this->temp_aux);
        $this->engine->display('index.tpl');
        $this->engine->display('footer.tpl');
    }

    public function run() {
        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
        }
        $this->display();
    }

}

$call = new c_index();
$call->run();
?>